package com.shangxia.lucky.lucky.job;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.shangxia.lucky.lucky.common.FactoryHead;
import com.shangxia.lucky.lucky.entity.Model;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @ProjectName: monomer
 * @ClassName: OverResult
 * @Time: 2021/9/12
 * @Author: zhouy
 * @Description:
 * @Experience:
 */
@Slf4j
@Component
public class OverResult extends BaseJob {

    @Override
    public void exportLstOverRowResult(List<List<Integer>> rowLastList, int size) {
        super.exportLstOverRowResult(rowLastList, size);
        exportLastRow(rowLastList);
    }

    public void exportLastRow(List<List<Integer>> rowLastList) {
        if (!configText.getBananaOverLastExportOpen() || CollectionUtils.isEmpty(rowLastList)) {
            return;
        }
        List<List<Integer>> data = new ArrayList<>();
        for (int index = 0; index < rowLastList.size(); index++) {
            int num = index;
            List<Integer> list = new ArrayList<>();
            List<Integer> temp = new ArrayList<>();
            if (index == 0) {
                data.add(rowLastList.get(0));
                continue;
            }
            while (num > 0) {
                if (CollectionUtils.isEmpty(list)) {
                    for (Integer indexNow : rowLastList.get(num)) {
                        list.add(rowLastList.get(num - 1).get(indexNow - 1));
                    }
                } else {
                    for (Integer indexNow : list) {
                        temp.add(rowLastList.get(num - 1).get(indexNow - 1));
                    }
                    list.clear();
                    list.addAll(temp);
                    temp.clear();
                }
                num--;
            }
            data.add(list);
        }
        String overFile = configText.getBananaAbsoluteResultPath() + DateFormatUtils.format(new Date(), "HH-mm-ss") + "_翻层最后一层表.xlsx";
        ExcelWriter excelWriter = EasyExcel.write(overFile).head(FactoryHead.overLast(configText.getBananaCodeLength())).build();
        excelWriter.write(changeListsInt2Str(data), EasyExcel.writerSheet(0, "最后一层").build());
        excelWriter.finish();
    }

    @Override
    public List<List<String>> changeListsInt2Str(List<List<Integer>> data) {
        List<List<String>> dataStr = new ArrayList<>();
        int row = 1;
        for (List<Integer> listI : data) {
            List<String> listStr = new ArrayList<>();
            listStr.add(0, "第" + (row++) + "层");
            listI.stream().forEach(e -> {
                listStr.add("" + e);
            });
            dataStr.add(listStr);
        }
        return dataStr;
    }

    @Override
    public void logOver() {
        log.info("-------------------------");
        log.info("最后一层 输出结束");
        log.info("-------------------------");
    }

    @Override
    public void initLAST_LIST(Integer column) {

    }

    @Override
    public void lastRowData(List<Model> resultList) {

    }

    @Override
    public void exportLastRowResult() {

    }

    @Override
    public void exportRowResult(Map<String, Object> params) {

    }

    @Override
    public void doPrivateJob(Map<String, Object> params, List<List<Integer>> realCodeLists) {

    }


}
